package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.domain.dao.CorePollingEquipmentMapper;
import com.corechina.apex_osswords.domain.dao.CorePollingPlanMapper;
import com.corechina.apex_osswords.domain.entity.CorePolling;
import com.corechina.apex_osswords.domain.entity.CorePollingPlan;
import com.corechina.apex_osswords.domain.vo.CorePollingPlanVo;
import com.corechina.apex_osswords.service.ICorePollingPlanService;
import com.corechina.apex_osswords.service.ICorePollingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 巡检计划表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-03-09
 */
@Service
public class CorePollingPlanServiceImpl extends ServiceImpl<CorePollingPlanMapper, CorePollingPlan> implements ICorePollingPlanService {

    @Autowired
    private CorePollingPlanMapper corePollingPlanMapper;

    @Autowired
    private ICorePollingService iCorePollingService;

    @Autowired
    private CorePollingEquipmentMapper corePollingEquipmentMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response add(CorePollingPlan corePollingPlan) {
        CorePolling corePolling = iCorePollingService.getById(corePollingPlan.getParentId());
        if (corePolling == null) {
            return Response.error("巡检不存在");
        }
        corePollingPlan.setStatus(0);
        corePollingPlanMapper.insert(corePollingPlan);
        iCorePollingService.addEquipment(corePolling.getEquipmentList().split(","), corePollingPlan.getId());
        return Response.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response del(Integer id) {
        CorePollingPlan corePollingPlan = corePollingPlanMapper.selectById(id);
        if (!corePollingPlan.getStatus().equals(0)) {
            return Response.error("巡检已开始，不能删除");
        }
        corePollingEquipmentMapper.deleteByPlanId(id);
        corePollingPlanMapper.deleteById(id);
        return Response.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response update(CorePollingPlan corePollingPlan) {
        CorePollingPlan dbCorePollingPlan = corePollingPlanMapper.selectById(corePollingPlan.getId());
        if (!dbCorePollingPlan.getStatus().equals(0)) {
            return Response.error("巡检已开始，不能修改");
        }
        corePollingPlan.setStatus(null);
        corePollingPlan.setParentId(null);
        corePollingPlanMapper.updateById(corePollingPlan);
        return Response.ok();
    }

    @Override
    public Response getList(Integer abnormal, Integer pollingId, Integer enterpriseId, Integer status, String planPersonName, Integer planPerson, Integer isToday, String beginDate, String endDate, Integer pageIndex, Integer pageSize) {
        IPage<CorePollingPlanVo> corePollingPlanVoIPage = corePollingPlanMapper.getList(new Page(pageIndex, pageSize), abnormal, pollingId, enterpriseId, status, planPersonName, planPerson, isToday, beginDate, endDate);
        return Response.ok(corePollingPlanVoIPage.getRecords(), corePollingPlanVoIPage.getTotal());
    }

    @Override
    public Response getListByProjectIdForWeiXin(String beginDate, String endDate, Integer projectId, Integer status, Integer enterpriseId, Integer pageIndex, Integer pageSize) {
        IPage<CorePollingPlanVo> corePollingPlanVoIPage = corePollingPlanMapper.getListByProjectIdForWeiXin(new Page(pageIndex, pageSize), beginDate, endDate, projectId, status, enterpriseId);
        return Response.ok(corePollingPlanVoIPage.getRecords(), corePollingPlanVoIPage.getTotal());
    }

    @Override
    public Response getOne(Integer id) {
        return Response.ok(corePollingPlanMapper.getOne(id));
    }
}
